package com.crm.system.service.impl.memberManager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.dto.InsertAloneMemberCardCallable;
import com.crm.model.dto.InsertContactCallable;
import com.crm.model.dto.InsertMemberCardCallable;
import com.crm.model.dto.InsertTaxCardCallable;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactErrorRecord;
import com.crm.model.entity.contactManager.ContactInformation;
import com.crm.model.entity.contractManager.CustomerContract;
import com.crm.model.entity.customerManager.*;
import com.crm.model.entity.finishOrderProduct.FinishOrderProduct;
import com.crm.model.entity.memberManager.CustomerMemberCardOld;
import com.crm.model.entity.returnMoney.ReturnMoney;
import com.crm.model.entity.sys.ImportFailureLog;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.memberManager.MemberShipCardQO;
import com.crm.model.vo.memberManager.MemberCardOldVO;
import com.crm.model.vo.memberManager.MemberShipCardListVO;
import com.crm.model.vo.orderManager.CustomerProductOrderVO;
import com.crm.model.vo.systemManager.*;
import com.crm.service.common.DictionaryDbService;
import com.crm.service.contractManager.CustomerContractDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.customerManager.CustomerPayDbService;
import com.crm.service.customerManager.CustomerProductDetailMemberCardDbService;
import com.crm.service.memberManager.MemberCardDbService;
import com.crm.service.orderManager.CustomerProductOrderDbService;
import com.crm.service.productManager.CustomerMemberProductDbService;
import com.crm.service.returnMoney.FinishOrderDBservice;
import com.crm.service.returnMoney.ReturnMoneyDbservice;
import com.crm.service.sys.ImportFailureLogDbService;
import com.crm.service.sys.UserAccountDbService;
import com.crm.service.sys.permissionManagement.SysCompanyDbService;
import com.crm.system.service.memberManager.MemberShipCardImportService;
import com.crm.system.service.memberManager.MemberShipCardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;

/**
 * @author luojie
 * @date 2021/4/8 13:53
 */
@Service
@Slf4j
public class memberCardServiceImpl implements MemberShipCardService {

    @Autowired
    private MemberCardDbService memberCardDbService;

    @Autowired
    private UserAccountDbService userAccountDbService;

    @Autowired
    private ImportFailureLogDbService importFailureLogDbService;

    @Autowired
    private CustomerDbService customerDbService;

    @Autowired
    private CustomerContractDbService customerContractDbService;

    @Autowired
    private CustomerProductOrderDbService customerProductOrderDbService;

    @Autowired
    private CustomerProductDetailMemberCardDbService customerProductDetailMemberCardDbService;

    @Autowired
    private FinishOrderDBservice finishOrderDBservice;

    @Autowired
    private CustomerMemberProductDbService customerMemberProductDbService;

    @Autowired
    private DictionaryDbService dictionaryDbService;

    @Autowired
    private CustomerPayDbService customerPayDbService;

    @Autowired
    private ReturnMoneyDbservice returnMoneyDbservice;

    @Autowired
    private SysCompanyDbService sysCompanyDbService;

    @Autowired
    private CustomerContractDbService contractDbService;

    /***
     * @Description:查询会员卡列表
     * @Param: memberShipCardQO 查询条件
     * @Param: pageIndex/pageSize  页号/每页显示个数
     * @Param: sortName/sortOrder  ---排序字段及规则
     * @Author: luojie
     * @Date: 2021/4/8 13:57
     */
    @Override
    public DhPageInfo<MemberShipCardListVO> selectMemberShipCardList(MemberShipCardQO memberShipCardQO, Integer pageIndex, Integer pageSize, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<MemberShipCardListVO> memberShipCardListVOList = memberCardDbService.selectMemberShipCardList(memberShipCardQO);
        PageInfo<MemberShipCardListVO> pageInfo = new PageInfo<>(memberShipCardListVOList);
        return new DhPageInfo<MemberShipCardListVO>(pageInfo, memberShipCardListVOList);
    }

    /**
     * 导入会员卡信息
     * @param: [list, companyId] 导入的集合,公司id
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: baijian
     * @Date: 2021/6/24
     */
    @Override
    public JSONObject importMembershipCard(List<MemberShipCardImportVO> list, String companyId) {
        //返回的结果集
        JSONObject jsonObject = new JSONObject();
        //导入数量
        int importNum = 0;
        //重复的行数
        String repeatIdStr = "";
        //失败行数
        String importFailIdStr = "";

        if (list.size() > 0){
            int count = 300;                   //一个线程处理300条数据
            int listSize = list.size();        //数据集合大小
            int runSize = (listSize/count)+1;  //开启的线程数
            //int runSize = importExportService.calculateImportedThreads(listSize);

            List<MemberShipCardImportVO> newlist = null;       //存放每个线程的执行数据

            ExecutorService executor = Executors.newCachedThreadPool();
            //创建两个个计数器
            CountDownLatch begin = new CountDownLatch(1);
            CountDownLatch end = new CountDownLatch(runSize);
            List<FutureTask<JSONObject>> taskList = new ArrayList<FutureTask<JSONObject>>();
            for (int i = 0; i < runSize ; i++) {
                //计算每个线程执行的数据
                if ((i + 1) == runSize) {
                    int startIndex = (i * count);
                    int endIndex = list.size();
                    newlist = list.subList(startIndex, endIndex);
                } else {
                    int startIndex = (i * count);
                    int endIndex = (i + 1) * count;
                    if (endIndex > list.size()){
                        endIndex = list.size();
                    }
                    newlist = list.subList(startIndex, endIndex);
                }
                FutureTask<JSONObject> futureTask = new FutureTask(new InsertMemberCardCallable(
                        customerDbService, userAccountDbService,
                        customerContractDbService,customerProductOrderDbService,
                        customerProductDetailMemberCardDbService,finishOrderDBservice,
                        customerMemberProductDbService,dictionaryDbService,
                        importFailureLogDbService,
                        newlist, begin, end,companyId));
                taskList.add(futureTask);
                executor.submit(futureTask);//submit返回一个Future，代表了即将要返回的结果
            }
            //重复的行数
            List<Integer> repeatIdList = new ArrayList();
            //失败行数
            List<Integer> importFailIdList = new ArrayList();

            for (int i = 0; i < taskList.size(); i++) {
                try {
                    // FutureTask的get方法会自动阻塞,直到获取计算结果为止
                    //导入数量
                    importNum = importNum + taskList.get(i).get().getInteger("importNum");

                    //重复数量
                    JSONArray repeatIdArray = taskList.get(i).get().getJSONArray("repeatIdStr");
                    for (int j = 0; j < repeatIdArray.size(); j++){
                        int repeat = Integer.parseInt(repeatIdArray.get(j).toString());
                        repeatIdList.add(repeat + i*100);
                    }

                    //失败数量
                    JSONArray importFailIdArray = taskList.get(i).get().getJSONArray("importFailIdStr");
                    for (int j = 0; j < importFailIdArray.size(); j++){
                        int importFail = Integer.parseInt(importFailIdArray.get(j).toString());
                        importFailIdList.add(importFail + i*100);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }

            // 关闭线程池
            executor.shutdown();

            jsonObject.put("importCallNum", importNum);
            jsonObject.put("repeatIdStr", repeatIdList);
            jsonObject.put("importFailIdStr", importFailIdList);

        }
        return jsonObject;
    }

    /**
     * 导入会员卡信息
     * @param: [list, companyId] 导入的集合,公司id
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: baijian
     * @Date: 2021/6/24
     */
    @Override
    public JSONObject importAloneMembershipCard(List<AloneMemberShipCardImportVO> list) {
        //返回的结果集
        JSONObject jsonObject = new JSONObject();
        //导入数量
        int importNum = 0;

        if (list.size() > 0){
            int count = 300;                   //一个线程处理300条数据
            int listSize = list.size();        //数据集合大小
            int runSize = (listSize/count)+1;  //开启的线程数
            //int runSize = importExportService.calculateImportedThreads(listSize);

            List<AloneMemberShipCardImportVO> newlist = null;       //存放每个线程的执行数据

            ExecutorService executor = Executors.newCachedThreadPool();
            //创建两个个计数器
            CountDownLatch begin = new CountDownLatch(1);
            CountDownLatch end = new CountDownLatch(runSize);
            List<FutureTask<JSONObject>> taskList = new ArrayList<FutureTask<JSONObject>>();
            for (int i = 0; i < runSize ; i++) {
                //计算每个线程执行的数据
                if ((i + 1) == runSize) {
                    int startIndex = (i * count);
                    int endIndex = list.size();
                    newlist = list.subList(startIndex, endIndex);
                } else {
                    int startIndex = (i * count);
                    int endIndex = (i + 1) * count;
                    if (endIndex > list.size()){
                        endIndex = list.size();
                    }
                    newlist = list.subList(startIndex, endIndex);
                }
                FutureTask<JSONObject> futureTask = new FutureTask(new InsertAloneMemberCardCallable(
                        customerDbService, userAccountDbService,sysCompanyDbService,
                        customerContractDbService,customerProductOrderDbService,
                        customerProductDetailMemberCardDbService,finishOrderDBservice,
                        newlist, begin, end));
                taskList.add(futureTask);
                executor.submit(futureTask);//submit返回一个Future，代表了即将要返回的结果
            }
            //重复的行数
            List<Integer> repeatIdList = new ArrayList();
            //失败行数
            List<Integer> importFailIdList = new ArrayList();

            for (int i = 0; i < taskList.size(); i++) {
                try {
                    // FutureTask的get方法会自动阻塞,直到获取计算结果为止
                    //导入数量
                    importNum = importNum + taskList.get(i).get().getInteger("importNum");

                    //重复数量
                    JSONArray repeatIdArray = taskList.get(i).get().getJSONArray("repeatIdStr");
                    for (int j = 0; j < repeatIdArray.size(); j++){
                        int repeat = Integer.parseInt(repeatIdArray.get(j).toString());
                        repeatIdList.add(repeat + i*100);
                    }

                    //失败数量
                    JSONArray importFailIdArray = taskList.get(i).get().getJSONArray("importFailIdStr");
                    for (int j = 0; j < importFailIdArray.size(); j++){
                        int importFail = Integer.parseInt(importFailIdArray.get(j).toString());
                        importFailIdList.add(importFail + i*100);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }

            // 关闭线程池
            executor.shutdown();

            jsonObject.put("importCallNum", importNum);
            jsonObject.put("repeatIdStr", repeatIdList);
            jsonObject.put("importFailIdStr", importFailIdList);

        }
        return jsonObject;
    }

    /**
     * 导入会员卡信息
     * @param: [list, companyId] 导入的集合,公司id
     * @return: com.alibaba.fastjson.JSONObject
     * @Author: baijian
     * @Date: 2021/6/24
     */
    @Override
    public JSONObject importTaxCard(List<TaxCardImportVO> list, String companyId) {
        //返回的结果集
        JSONObject jsonObject = new JSONObject();
        //导入数量
        int importNum = 0;
        //重复的行数
        String repeatIdStr = "";
        //失败行数
        String importFailIdStr = "";

        if (list.size() > 0){
            int count = 500;                 //一个线程处理300条数据
            int listSize = list.size();        //数据集合大小
            int runSize = (listSize/count)+1;  //开启的线程数
            //int runSize = importExportService.calculateImportedThreads(listSize);

            List<TaxCardImportVO> newlist = null;       //存放每个线程的执行数据

            ExecutorService executor = Executors.newCachedThreadPool();
            //创建两个个计数器
            CountDownLatch begin = new CountDownLatch(1);
            CountDownLatch end = new CountDownLatch(runSize);
            List<FutureTask<JSONObject>> taskList = new ArrayList<FutureTask<JSONObject>>();
            for (int i = 0; i < runSize ; i++) {
                //计算每个线程执行的数据
                if ((i + 1) == runSize) {
                    int startIndex = (i * count);
                    int endIndex = list.size();
                    newlist = list.subList(startIndex, endIndex);
                } else {
                    int startIndex = (i * count);
                    int endIndex = (i + 1) * count;
                    if (endIndex > list.size()){
                        endIndex = list.size();
                    }
                    newlist = list.subList(startIndex, endIndex);
                }
                FutureTask<JSONObject> futureTask = new FutureTask(new InsertTaxCardCallable(
                        customerDbService, userAccountDbService,
                        customerProductOrderDbService,
                        finishOrderDBservice,
                        newlist, begin, end,companyId));
                taskList.add(futureTask);
                executor.submit(futureTask);//submit返回一个Future，代表了即将要返回的结果
            }
            //重复的行数
            List<Integer> repeatIdList = new ArrayList();
            //失败行数
            List<Integer> importFailIdList = new ArrayList();

            for (int i = 0; i < taskList.size(); i++) {
                try {
                    // FutureTask的get方法会自动阻塞,直到获取计算结果为止
                    //导入数量
                    importNum = importNum + taskList.get(i).get().getInteger("importNum");

                    //重复数量
                    JSONArray repeatIdArray = taskList.get(i).get().getJSONArray("repeatIdStr");
                    for (int j = 0; j < repeatIdArray.size(); j++){
                        int repeat = Integer.parseInt(repeatIdArray.get(j).toString());
                        repeatIdList.add(repeat + i*100);
                    }

                    //失败数量
                    JSONArray importFailIdArray = taskList.get(i).get().getJSONArray("importFailIdStr");
                    for (int j = 0; j < importFailIdArray.size(); j++){
                        int importFail = Integer.parseInt(importFailIdArray.get(j).toString());
                        importFailIdList.add(importFail + i*100);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }

            // 关闭线程池
            executor.shutdown();

            jsonObject.put("importCallNum", importNum);
            jsonObject.put("repeatIdStr", repeatIdList);
            jsonObject.put("importFailIdStr", importFailIdList);

            log.info("---------" + importNum);

            for(Integer a : repeatIdList) {
                log.info("修改的---------" + a);
            }

            for(Integer a : importFailIdList) {
                log.info("错误的---------" + a);
            }
        }
        return jsonObject;
    }

    /***
     * @Description: 查询旧系统会员卡列表
     * @Param:
     * @Author: luojie
     * @Date: 2021/4/8 13:41
     */
    @Override
    public DhPageInfo<MemberCardOldVO> selectMemberCardOldList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, MemberShipCardQO memberShipCardQO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<MemberCardOldVO> list = memberCardDbService.selectMemberCardOldList(memberShipCardQO);
        PageInfo<MemberCardOldVO> pageInfo = new PageInfo<>(list);
        return new DhPageInfo<MemberCardOldVO>(pageInfo, list);
    }

    /***
     * @Description: 查询旧系统公司名称
     * @Param:
     * @Author: luojie
     * @Date: 2021/4/8 13:41
     */
    @Override
    public List<MemberCardOldVO> getCompanyList() {
        return memberCardDbService.getCompanyList();
    }

    /**
     * 删除充值码
     * @return
     * @throws IOException
     */
    @Override
    public void updateRechargeCodeDelete(List<RechargeCodeImportVO> list){
        int i = 0;
        for(RechargeCodeImportVO card :list) {
            CustomerProductDetailRechargeCode rechargeCode = customerDbService.selectRechargeCode(card.getCard());
            if(rechargeCode != null) {
                i = i+1;
                rechargeCode.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
                customerDbService.updateRechargeCode(rechargeCode);
            }
        }
        log.info("---------------------------"+i);
    }

    /***
     * 补全会员卡开始日期和结束时间
     */
    @Override
    public void  updateCardDate() {
        int i = 0;
        Date date = new Date();
        List<MemberCardOldVO> list =memberCardDbService.selectCardDate();
        for(MemberCardOldVO vo : list) {
            CustomerContract customerContract = contractDbService.selectContractByOrderId(vo.getProductOrderId());
            if(customerContract != null) {
                if(customerContract.getContractStartDate() != null && customerContract.getContractEndDate() != null) {
                    CustomerProductDetailMemberCard card = new CustomerProductDetailMemberCard();
                    card.setCustomerProductDetailMemberCardId(vo.getMemberCardId());
                    card.setStartTm(customerContract.getContractStartDate());
                    card.setEndTm(customerContract.getContractEndDate());
                    card.setUpdateTm(date);
                    customerDbService.updateCustomerProductDetailMemberCard(card);
                    i = i+1;
                }
            }
        }
        log.info("------------------"+i);
    }

    /**
     * 删除兑换码
     * @return
     * @throws IOException
     */
    @Override
    public void updateExchangeCodeDelete(List<RechargeCodeImportVO> list) {
        int i = 0;
        for(RechargeCodeImportVO card :list) {
            CustomerProductDetailExchangeCode exchangeCode = customerDbService.selectExchangeCode(card.getCard());
            if(exchangeCode != null) {
                i = i+1;
                exchangeCode.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
                customerDbService.updateExchangeCode(exchangeCode);
            }
        }
        log.info("---------------------------"+i);
    }

    /**
     *补全会员卡客户id
     * @param list
     * @return
     */
    @Override
    public JSONObject importCard(List<CardVO> list) {
        Date date = new Date();
        Set<String> sList = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            CardVO vo = list.get(i);
            CustomerProductDetailMemberCard card = customerDbService.selectCardByNumber(vo.getCardNumber());
            if(card != null) {
                if(!StringUtils.isBlank(vo.getFirm())) {
                    Customer customer = customerDbService.selectByCustomerName(vo.getFirm());
                    if(customer ==null) {
                        if(!StringUtils.isBlank(vo.getFirms())) {
                            Customer cust = customerDbService.selectByCustomerName(vo.getFirms());
                            if(cust == null) {
                                if(!StringUtils.isBlank(vo.getCustomerNumber())) {
                                    Customer cu = customerDbService.selectByCustomerNumber(vo.getCustomerNumber());
                                    if(cu == null) {
                                        sList.add(vo.getCardNumber());
                                    }else{
                                        updateCard(card,cu,date);
                                    }
                                }else{
                                    sList.add(vo.getCardNumber());
                                }
                            }else{
                                updateCard(card,cust,date);
                            }
                        }else{
                            if(!StringUtils.isBlank(vo.getCustomerNumber())) {
                                Customer cu = customerDbService.selectByCustomerNumber(vo.getCustomerNumber());
                                if(cu == null) {
                                    sList.add(vo.getCardNumber());
                                }else{
                                    updateCard(card,cu,date);
                                }
                            }else{
                                sList.add(vo.getCardNumber());
                            }
                        }
                    }else{
                        updateCard(card,customer,date);
                    }
                }else{
                    if(!StringUtils.isBlank(vo.getFirms())) {
                        Customer cust = customerDbService.selectByCustomerName(vo.getFirms());
                        if(cust == null) {
                            if(!StringUtils.isBlank(vo.getCustomerNumber())) {
                                Customer cu = customerDbService.selectByCustomerNumber(vo.getCustomerNumber());
                                if(cu == null) {
                                    sList.add(vo.getCardNumber());
                                }else{
                                    updateCard(card,cu,date);
                                }
                            }else{
                                sList.add(vo.getCardNumber());
                            }
                        }else{
                            updateCard(card,cust,date);
                        }
                    }else{
                        if(!StringUtils.isBlank(vo.getCustomerNumber())) {
                            Customer cu = customerDbService.selectByCustomerNumber(vo.getCustomerNumber());
                            if(cu == null) {
                                sList.add(vo.getCardNumber());
                            }else{
                                updateCard(card,cu,date);
                            }
                        }else {
                            sList.add(vo.getCardNumber());
                        }
                    }
                }

                if(StringUtils.isBlank(vo.getFirm()) && StringUtils.isBlank(vo.getFirms()) && StringUtils.isBlank(vo.getCustomerNumber())) {
                    sList.add(vo.getCardNumber());
                }
            }
        }
        for (String a :sList) {
            log.info("-----------------"+a);
        }
        return null;
    }

    public void updateCard(CustomerProductDetailMemberCard card,Customer cust,Date date) {
        card.setCustomerId(cust.getCustomerId());
        card.setUpdateTm(date);
        customerDbService.updateCustomerProductDetailMemberCard(card);
    }

    /**
     * 导入客户充值信息
     * @param list
     * @return
     */
    @Override
    public JSONObject importCustomerPay(List<CustomerPayImportVO> list) {
            //导入数量
            int importNum = 0;
            //失败行数
            List<String> userList = new ArrayList<>();
            List<String> customerList = new ArrayList<>();
            List<String> payList = new ArrayList<>();
            JSONObject jsonObject = new JSONObject();
            Date date = new Date();
            for (int i = 0; i < list.size(); i++) {
                CustomerPayImportVO importVO = list.get(i);
                UserAccount userAccount = null;
                try {
                        //查询账号是否存在
                        Customer customer = null;
                        if (!StringUtils.isBlank(importVO.getUserName())) {
                            userAccount = userAccountDbService.selectUserAccountByUserName(importVO.getUserName());
                        }

                        if (userAccount != null) {
                            if (!StringUtils.isBlank(importVO.getCustomerName())) {
                                customer = customerDbService.selectCustomerNameAndAccountId(userAccount.getAccountId(), importVO.getCustomerName());
                            }

                            if (!StringUtils.isBlank(customer.getCustomerId()) && customer != null) {
                                //正确的
                                int aa = aa(customer, importVO, userAccount, date, importNum);
                                importNum = importNum + aa;
                            } else {
                                customer = customerDbService.selectByCustomerName(importVO.getCustomerName());
                                if(customer != null) {
                                    //客户与所有人对不上的
                                    int aa =  aa(customer, importVO,userAccount,date,importNum);
                                    importNum = importNum + aa;
                                }else{
                                    int aa =  aa(customer, importVO,userAccount,date,importNum);
                                    importNum = importNum + aa;
                                    //userList.add(importVO.getCustomerName());
                                }

                            }

                        } else {
                            customer = customerDbService.selectByCustomerName(importVO.getCustomerName());
                            if(customer != null) {
                                //所有人没有的
                                int aa = aa(customer, importVO,userAccount,date,importNum);
                                importNum = importNum + aa;
                            }else {
                                int aa =  aa(customer, importVO,userAccount,date,importNum);
                                importNum = importNum + aa;
                            }


                        }
                } catch (Exception e) {
                    log.error("导入会员卡异常, Exception info : {}", e);
                }
            }

            for(String user :userList) {
                log.info("用户查不到的----" + user);
            }

            /*for(String user :customerList) {
                log.info("客户不是所有人的----" + user);
            }*/

            /*for(String user : payList) {
                log.info("充值记录存在的----" + user);
            }*/


            log.info("导入----" + importNum);
            jsonObject.put("importNum",importNum);
            return jsonObject;
        }

    public Date formatTime(String date) {
        //同一个Excel有不一样的时间格式……
        try{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return simpleDateFormat.parse(date);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int aa(Customer customer,CustomerPayImportVO importVO,UserAccount userAccount,Date date,int num) {
        List<CustomerPay> customerPays = new ArrayList<>();
        if(customer != null) {
            customerPays = customerPayDbService.selectCustomerId(customer.getCustomerId(),new BigDecimal(importVO.getCustomerPay()));
        }

        if(customerPays.size() == 0) {
            CustomerPay pay = new CustomerPay();
            pay.setChsCustomerPayId(CommonUtils.getGUID());
            pay.setPayNumber("F"+String.valueOf(new Date().getTime()));
            if(customer != null) {
                pay.setCustomerId(customer.getCustomerId());
            }
            pay.setPayMoney(new BigDecimal(importVO.getCustomerPay()));
            pay.setPayType(1);
            pay.setPayStatus(1);
            pay.setArriveAccountType(1);
            pay.setPaymentUnit(importVO.getCustomerName());
            pay.setPaymentDate(formatTime(importVO.getDkrq()));
            pay.setArriveMethod(0);
            pay.setRemark("代码补录...");
            if(userAccount != null ) {
                pay.setCreatePeopleId(userAccount.getAccountId());
            }
            pay.setCreateTm(formatTime(importVO.getDkrq()));
            pay.setUpdatePeopleId("71902468-8dee-4e30-b554-456d2140c7d9");
            pay.setUpdateTm(date);
            customerPayDbService.saveCustomerPay(pay);

            if(customer != null) {
                customer.setCustomerWallet(customer.getCustomerWallet().add(new BigDecimal(importVO.getCustomerPay())));
                customer.setUpdateTm(date);
                customerDbService.updateByPrimaryKeySelective(customer);
            }



            if("已发提成".equals(importVO.getTcffzt())) {
                if(!StringUtils.isBlank(importVO.getOrderNumber())) {
                    CustomerProductOrderVO order = customerProductOrderDbService.getOrderInfoByNumber(importVO.getOrderNumber());
                    if(order != null) {
                        List<ReturnMoney> returns = returnMoneyDbservice.selectMoneyByOrderId(order.getProductOrderId());
                        if (returns != null && returns.size() > 0) {
                            ReturnMoney returnMoney = returns.get(0);
                            returnMoney.setRoyaltyStatus(1);
                            returnMoney.setUpdateTm(date);
                            returnMoneyDbservice.updateReturnMoney(returnMoney);
                        }
                    }
                }
            }

            num +=1;
        }else{
            //payList.add(importVO.getCustomerName());
        }
        return num;
    }


}
